<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :lg="7">
        <el-card class="box-card" :style="'height:' + height">
          <myArea :device_this="sup_this"/>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="17">
        <el-card class="box-card" :style="'height:' + height">
          <eHeader :query="query" :dict="dict" :sup_this="sup_this"/>
          <!--表格渲染-->
          <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
            <el-table-column prop="deviceName" label="设备名称"/>
            <el-table-column prop="deviceIp" label="设备IP"/>
            <el-table-column prop="devicePort" label="设备端口"/>
            <el-table-column prop="devicePoint" label="节点地址"/>
            <el-table-column label="设备区域">
              <template slot-scope="scope">
                <div v-for="item in dict.area" :key="item.id">
                  <span v-if="scope.row.deviceArea==item.id">{{ item.areaName }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="refreshRate" label="刷新频率"/>
            <el-table-column prop="deviceNum" label="设备编号"/>
            <el-table-column prop="deviceType" label="设备类型"/>
            <el-table-column prop="deviceModel" label="设备型号"/>
            <el-table-column prop="deviceGrade" label="设备级别"/>
            <el-table-column prop="deviceBrand" label="设备品牌"/>
            <el-table-column prop="manufacturer" label="设备厂商"/>
            <el-table-column label="操作" width="150px" align="center">
              <template slot-scope="scope">
                <edit :dict="dict" :data="scope.row" :sup_this="sup_this"/>
                <el-popover
                  :ref="scope.row.id"
                  placement="top"
                  width="180">
                  <p>确定删除本条数据吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                    <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
                    </el-button>
                  </div>
                  <el-button slot="reference" type="danger" size="mini">删除</el-button>
                </el-popover>
              </template>
            </el-table-column>
          </el-table>
          <!--分页组件-->
          <el-pagination
            :total="total"
            style="margin-top: 8px;"
            layout="total, prev, pager, next, sizes"
            @size-change="sizeChange"
            @current-change="pageChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { del } from '@/api/environmentalDevice'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import myArea from '@/views/system/area'
import {getDict} from '@/api/accessDevice'
export default {
  components: {eHeader, edit,myArea},
  mixins: [initData],
  data() {
    return {
      height: document.documentElement.clientHeight - 115 + 'px;',
      delLoading: false, sup_this: this,
      dict: {}
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
      this.getDict()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    getDict(){
      getDict().then(res=>{
        this.dict = res
      }).catch(err=>{
        console.log(err.response.data.message)
      })
    },
    beforeInit() {
      this.url = 'api/environmentalDevice'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const deviceName = query.deviceName
      const deviceArea = query.deviceArea
      if (deviceName) {
        this.params['deviceName'] = deviceName
      }
      if (deviceArea) {
        this.params['deviceArea'] = deviceArea
      }
      return true
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
